﻿using System;
using System.Collections.Generic;

using Gonte.Logging;

using Microsoft.VisualStudio.TestTools.UnitTesting;

using MyUtilitiesTest.Logging;

namespace MyUtilitiesTest
{
    /// <summary>
    ///This is a test class for LoggerTest and is intended
    ///to contain all LoggerTest Unit Tests
    ///</summary>
    [TestClass()]
    public class LoggerTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        public static bool ContainsMessage(Logger.LogLevel level, String message)
        {
            InMemoryTraceListener listener = (InMemoryTraceListener)Logger.Configuration.Listeners["TestTracer"];
            String msg = String.Format("[{0}] - {1}{2}", level.ToString(), message, Environment.NewLine);

            return listener.Messages.Contains(msg);
        }

        public class TestLogger1
        {
            public void Test()
            {
                // Automatic detection 
                // No formatted
                Logger.Log(Logger.LogLevel.Information, "TestLogger1 (Automatic no formatted) : Information");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Automatic no formatted) : Information"));

                Logger.Log(Logger.LogLevel.Warning, "TestLogger1 (Automatic no formatted) : Warning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Automatic no formatted) : Warning"));

                Logger.Log(Logger.LogLevel.Error, "TestLogger1 (Automatic no formatted : Error");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Automatic no formatted : Error"));

                // Formatted
                Logger.Log(Logger.LogLevel.Information, "TestLogger1 (Automatic formatted) : {0}", "Information");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Automatic formatted) : Information"));

                Logger.Log(Logger.LogLevel.Warning, "TestLogger1 (Automatic formatted) : {0}", "Warning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Automatic formatted) : Warning"));

                Logger.Log(Logger.LogLevel.Error, "TestLogger1 (Automatic formatted) : {0}", "Error");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Automatic formatted) : Error"));

                // No-formatted shortcuts
                Logger.LogInformation("TestLogger1 (Automatic no formatted) : LogInformation");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Automatic no formatted) : LogInformation"));

                Logger.LogWarning("TestLogger1 (Automatic no formatted) : LogWarning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Automatic no formatted) : LogWarning"));

                Logger.LogError("TestLogger1 (Automatic no formatted) : LogError");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Automatic no formatted) : LogError"));

                // Formatted shortcuts
                Logger.LogInformation("TestLogger1 (Automatic formatted) : {0}", "LogInformation");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Automatic formatted) : LogInformation"));

                Logger.LogWarning("TestLogger1 (Automatic formatted) : {0}", "LogWarning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Automatic formatted) : LogWarning"));

                Logger.LogError("TestLogger1 (Automatic formatted) : {0}", "LogError");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Automatic formatted) : LogError"));

                // Explicit detection
                // No formatted
                Logger.Log(GetType(), Logger.LogLevel.Information, "TestLogger1 (Explicit no formatted) : Information");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Explicit no formatted) : Information"));

                Logger.Log(GetType(), Logger.LogLevel.Warning, "TestLogger1 (Explicit no formatted) : Warning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Explicit no formatted) : Warning"));

                Logger.Log(GetType(), Logger.LogLevel.Error, "TestLogger1 (Explicit no formatted : Error");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Explicit no formatted : Error"));

                // Formatted
                Logger.Log(GetType(), Logger.LogLevel.Information, "TestLogger1 (Explicit formatted) : {0}", "Information");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Explicit formatted) : Information"));

                Logger.Log(GetType(), Logger.LogLevel.Warning, "TestLogger1 (Explicit formatted) : {0}", "Warning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Explicit formatted) : Warning"));

                Logger.Log(GetType(), Logger.LogLevel.Error, "TestLogger1 (Explicit formatted) : {0}", "Error");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Explicit formatted) : Error"));

                // No-formatted shortcuts
                Logger.LogInformation(GetType(), "TestLogger1 (Explicit no formatted) : LogInformation");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Explicit no formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger1 (Explicit no formatted) : LogWarning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Explicit no formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger1 (Explicit no formatted) : LogError");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Explicit no formatted) : LogError"));

                // Formatted shortcuts
                Logger.LogInformation(GetType(), "TestLogger1 (Explicit formatted) : {0}", "LogInformation");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger1 (Explicit formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger1 (Explicit formatted) : {0}", "LogWarning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger1 (Explicit formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger1 (Explicit formatted) : {0}", "LogError");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger1 (Explicit formatted) : LogError"));
            }
        }

        public class TestLogger2 // Only the information will be logged
        {
            public void Test()
            {
                Logger.LogInformation(GetType(), "TestLogger2 (Explicit formatted) : {0}", "LogInformation");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Information, "TestLogger2 (Explicit formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger2 (Explicit formatted) : {0}", "LogWarning"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Warning, "TestLogger2 (Explicit formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger2 (Explicit formatted) : {0}", "LogError"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Error, "TestLogger2 (Explicit formatted) : LogError"));
            }
        }

        public class TestLogger3 // Nothing will be logged
        {
            public void Test()
            {
                Logger.LogInformation(GetType(), "TestLogger3 (Explicit formatted) : {0}", "LogInformation"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Information, "TestLogger3 (Explicit formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger3 (Explicit formatted) : {0}", "LogWarning"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Warning, "TestLogger3 (Explicit formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger3 (Explicit formatted) : {0}", "LogError"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Error, "TestLogger3 (Explicit formatted) : LogError"));
            }
        }

        public class TestLogger4 // Only the warning will be logged
        {
            public void Test()
            {
                Logger.LogInformation(GetType(), "TestLogger4 (Explicit formatted) : {0}", "LogInformation"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Information, "TestLogger4 (Explicit formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger4 (Explicit formatted) : {0}", "LogWarning");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Warning, "TestLogger4 (Explicit formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger4 (Explicit formatted) : {0}", "LogError"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Error, "TestLogger4 (Explicit formatted) : LogError"));
            }
        }

        public class TestLogger5 // Only the error will be logged
        {
            public void Test()
            {
                Logger.LogInformation(GetType(), "TestLogger5 (Explicit formatted) : {0}", "LogInformation"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Information, "TestLogger5 (Explicit formatted) : LogInformation"));

                Logger.LogWarning(GetType(), "TestLogger5 (Explicit formatted) : {0}", "LogWarning"); // This won't be logged
                Assert.IsFalse(ContainsMessage(Logger.LogLevel.Warning, "TestLogger5 (Explicit formatted) : LogWarning"));

                Logger.LogError(GetType(), "TestLogger5 (Explicit formatted) : {0}", "LogError");
                Assert.IsTrue(ContainsMessage(Logger.LogLevel.Error, "TestLogger5 (Explicit formatted) : LogError"));
            }
        }

        [TestMethod()]
        public void LoggerLogTest()
        {
            TestLogger1 logger1 = new TestLogger1();
            logger1.Test();

            TestLogger2 logger2 = new TestLogger2();
            logger2.Test();

            TestLogger3 logger3 = new TestLogger3();
            logger3.Test();

            TestLogger4 logger4 = new TestLogger4();
            logger4.Test();

            TestLogger5 logger5 = new TestLogger5();
            logger5.Test();
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentException))]
        public void LogAllTest()
        {
            Logger.Log(Logger.LogLevel.All, "TestLogger5 (Explicit formatted) : {0}", "LogInformation"); // LogLevel.All does not make sense here
        }
    }
}
